Copper = {};   
Copper.Global = {
    centerX: 512,
    centerY: 378,
    clockRaius: 120,
    clockBorderWidth: 10,
    clockBorderRadius: 120 + 10,
    
    taskRadius: 75,
    currentTaskRadius: 155,
    taskArrowWidth: 10 / Math.sin(Math.PI / 3),
    
    bgRadius: 302,
    
    pointerWidth: 5,
    pointerLength: 20,
    
    currentDate: '20120610', //new Date(),
    currentTask:  -1,
    
    workMode: 'CLOCK',
    amPm: ''
};

Copper.Util = {    
    timeToAngle: function(time){
        time = time.split(':');      
        time = parseInt(time[0], 10) * 60 + parseInt(time[1], 10); 
        if(time >=0  && time < 180){
            return Math.PI * 1.5 + time / 720 * (Math.PI * 2);
        } else  {
            return (time - 180) / 720 * 2 * Math.PI; 
        }
        
    },
    
    angleToTime: function(angle){
        angle %= Math.PI * 4;
        if(angle < 0){
            angle += Math.PI * 2;
        }
        
        if(angle >= Math.PI * 1.5){
            time = (angle - Math.PI * 1.5) / (Math.PI * 2) * 720;
        } else {
            time = 180 + angle / Math.PI / 2 * 720;
        }
        time = Math.round(time);
        
        return Copper.Util.convertTimeToStr(time);
    },
    
    getPointAngle: function(x, y){
        x -= Copper.Global.centerX;
        y -= Copper.Global.centerY + $('#header').height(); 
        var angle = Math.atan(y / x);
        
        if(x >= 0){
            if(y >= 0){
                return Math.atan(y / x);    
            } else {
                return Math.PI * 2 - Math.atan(-y / x);
            }
        } else {
            if(y >= 0){
                return Math.PI + Math.atan(y / x);    
            } else {
                return Math.PI + Math.atan(y / x);
            }
        }
    },
    
    showScreenMask: function(){
        var mask = document.getElementById('screenMask');
        mask.style.display = 'block';
        mask.style.height = document.body.offsetHeight + 'px';
    },
    
    hideScreenMask: function(){
        document.getElementById('screenMask').style.display = 'none';
    },
    
    convertTimeToStr: function(time){
        var h = Math.floor(time / 60);
        var m = time % 60;
        return (h < 10? ('0' + h): h) + ':' + (m < 10? ('0' + m): m);         
    },
    
    convertStrToTime: function(str){
        str = str.split(':');
        return parseInt(str[0]) * 60 + parseInt(str[1]);
    },
    
    fixTime: function(startTime, endTime){
        startTime = Copper.Util.convertStrToTime(startTime);
        
        var m = startTime % 10;
        var n = 0;
        
        if(m < 3){
            n = -1 * m;
        } else if(m < 8){
            n = 5 - m;
        } else {
            n = 10 - m;
        }
        
        startTime += n;
        startTime = Copper.Util.convertTimeToStr(startTime);
        if(endTime){
            endTime = Copper.Util.convertStrToTime(endTime);
            endTime += n;
            endTime = Copper.Util.convertTimeToStr(endTime);
        }
        return [startTime, endTime];
    }
};

window.animStartTime = (function(w) {
  return  w.animationStartTime       || 
          w.webkitAnimationStartTime || 
          w.mozAnimationStartTime    || 
          w.oAnimationStartTime      || 
          w.msAnimationStartTime     || 
          +new Date();
})(window);